//
//  TabBarController.swift
//  FrameProduct
//
//  Created by delegate on 2024/2/4.
//

import UIKit

public enum MainTabBarItem: String, CaseIterable {
    case home = "首页"
    case mine = "我的"

    private func controller() -> UIViewController {
        switch self {
        case .home:
            let vc = HomeViewController()
            return NavigationController(rootViewController: vc)
        case .mine:
            let vc = MineViewController()
            return NavigationController(rootViewController: vc)
        }
    }

    var image: UIImage? {
        switch self {
        case .home: return UIImage(named: "icon_tabbar_home")
        case .mine: return UIImage(named: "icon_tabbar_home")
        }
    }
    
    var selectedImage: UIImage? {
        switch self {
        case .home: return UIImage(named: "icon_tabbar_home")
        case .mine: return UIImage(named: "icon_tabbar_home")
        }
    }
    
    var title: String { self.rawValue }
    
    
    public var tabBarItem: UITabBarItem {
        return UITabBarItem(title: title, image: image, selectedImage: selectedImage)
    }
    
    func getController() -> UIViewController {
        let vc = controller()
        vc.title = self.title
//        let item = UITabBarItem(title: self.title, image: image, selectedImage: selectedImage)
        vc.tabBarItem = tabBarItem
        return vc
    }
}

final class TabBarController: UITabBarController {
    deinit {
        debugLog(String(describing: self))
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
        addChildControllers()
    }
    // MARK: - Private methods
    private func setupUI() {
        let bgColor = UIColor.lxs.hex("#061637")
        let naviTextColor = UIColor.lxs.hex("#FFFFFF")
        
        //不设置背景图片
        tabBar.backgroundImage = UIImage(color: bgColor)
        
        //移除顶部线条
        tabBar.shadowImage = UIImage(color: bgColor)
        
        //添加阴影
        tabBar.layer.shadowColor = naviTextColor.cgColor
        // 设置阴影偏移量
        tabBar.layer.shadowOffset =  CGSize(width: 0, height: -1)//CGSize.zero
        // 设置阴影透明度
        tabBar.layer.shadowOpacity = 0.2
        // 设置阴影半径
        tabBar.layer.shadowRadius = 0
        
        tabBar.layer.masksToBounds = false
        
//        tabBar.tintColor = naviTextColor
        
        tabBarItem.setTitleTextAttributes([
            NSAttributedString.Key.foregroundColor: naviTextColor,
            NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18, weight: .medium)
        ], for: .normal)
        
        tabBarItem.setTitleTextAttributes([
            NSAttributedString.Key.foregroundColor: naviTextColor,
            NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18, weight: .medium)
        ], for: .selected)
        
        if #available(iOS 15.0, *) {
            let appearance = UITabBarAppearance()
            appearance.backgroundColor = bgColor
            appearance.backgroundImage = UIImage(color: bgColor)
            //移除顶部线条
            appearance.shadowImage = UIImage(color: bgColor)
            //添加阴影
            appearance.shadowColor = naviTextColor
            
            tabBar.scrollEdgeAppearance = appearance
            tabBar.standardAppearance = appearance
        }
    }
    private func addChildControllers() {
        _ = MainTabBarItem.allCases.map({ tabbarItem in
            addChild(tabbarItem.getController())
            return tabbarItem
        })
    }
}
